const sass = require('sass')
const loadGruntTasks = require('load-grunt-tasks')
const data = {
  menus: [
    {
      name: 'Home',
      icon: 'aperture',
      link: 'index.html'
    },
    {
      name: 'Features',
      link: 'features.html'
    },
    {
      name: 'About',
      link: 'about.html'
    },
    {
      name: 'Contact',
      link: '#',
      children: [
        {
          name: 'Twitter',
          link: 'https://twitter.com/w_zce'
        },
        {
          name: 'About',
          link: 'https://weibo.com/zceme'
        },
        {
          name: 'divider'
        },
        {
          name: 'About',
          link: 'https://github.com/zce'
        }
      ]
    }
  ],
  pkg: require('./package.json'),
  date: new Date()
}
module.exports = grunt => {
  grunt.initConfig({
    clean: ['tmp'],
    //静态服务器
    browserSync: {
      bsFiles: {
        src: [
          'tmp/assets/styles/*.css',
          'tmp/assets/scripts/*.js',
          'tmp/*.html'
        ],
      },
      options: {
        notify: false,
        port: 8080,
        watchTask: true,
        server: {
          baseDir: "./tmp",
          routes: {
            '/node_modules': 'node_modules'
          }
        },

      }
    },
    //解析html模板
    swigtemplates: {
      options: {
        templatesDir: 'src/'
      },
      production: {
        dest: 'tmp/',
        src: ['src/*.html']
      },
      staging: {
        context: {
          menus: data.menus,
          pkg: data.pkg,
          date: data.data
        },
        dest: 'tmp',
        src: ['src/*.html']
      }
    },
    //压缩图片
    imagemin: {
      tmp: {
        options: {
          optimizationLevel: 3 //定义 PNG 图片优化水平
        },
        files: [{
          expand: true,
          cwd: 'src/assets/images',   // 图片在imagemin目录下
          src: ['**/*.{png,jpg,jpeg,svg}'], // 优化 imagemin 目录下所有 png/jpg/jpeg 图片
          dest: 'tmp/assets/images' // 优化后的图片保存位置，覆盖旧图片，并且不作提示
        }]
      }
    },
    //复制font
    copy: {
      main: {
        files: [
          {
            cwd: 'src/assets/fonts',
            expand: true,
            src: '**', 
            dest: 'tmp/assets/fonts'
          }
        ]
      },
      public: {
        files: [
          {
            cwd: 'public/',
            expand: true,
            src: '**', 
            dest: 'tmp'
          }
        ]
      }
    },
    sass: {
      options: {
        sourceMap: true,
        implementation: sass
      },
      main: {
        files: [{
          cwd: 'src/assets/styles',
          expand: true,
          src: ['*.scss'],
          dest: 'tmp/assets/styles',
          ext: '.css'
        }]
      }
    },
    babel: {
      options: {
        sourceMap: true,
        presets: ['@babel/preset-env']
      },
      tmp: {
        files: {
          'tmp/assets/scripts/main.js': 'src/assets/scripts/main.js'
        }
      }
    },
    watch: {
      js: {
        files: [
          'src/assets/scripts/*.js'
        ],
        tasks: ['babel']
      },
      css: {
        files: [
          'src/assets/styles/*.scss'
        ],
        tasks: ['sass']
      },
      html: {
        files: [
          'src/**/*.html'
        ],
        tasks: ['swigtemplates']
      }
    }
  })
  loadGruntTasks(grunt); //自动加载所有grunt插件
  grunt.registerTask('default', ['clean', 'copy', 'swigtemplates', 'sass', 'babel', 'imagemin', 'browserSync','watch'])
}